#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "regist.h"

#include <QMessageBox>
#include <QCursor>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->label->setStyleSheet("background:transparent"); //label透明

    QString hostName = "122.51.25.191"; //云服务器
    QString dbName = "stu";
    QString userName = "root";
    QString password = "root";

    dbconn = QSqlDatabase::addDatabase("QMYSQL");
    dbconn.setHostName(hostName);
    dbconn.setDatabaseName(dbName);
    dbconn.setUserName(userName);
    dbconn.setPassword(password);

//    测试MySQL连接
//    qDebug("Database open status: %d.\n", dbconn.open());
//    QSqlError error = dbconn.lastError();
//    qDebug() << error.text();
//    dbconn.close();
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_actiontips_triggered()
{
    QMessageBox::information(this, "Tips", "学生用户名和密码均为学号，登录后可修改密码", "确定");
}

void MainWindow::on_actioncopyright_triggered()
{
    QMessageBox::information(this, "Copyright", "开发者:\nU201714814 赵泽阳\nU201714836 高志远\nU201714809 黄文早\n\nAll Right Reserved", "确定");
}


void MainWindow::on_mainwindow_regist_clicked()
{
    Regist *r;
    r = new Regist();
    r->setdbconn(&dbconn);
    setCursor(Qt::BusyCursor);
    if(dbconn.open()){
        setCursor(Qt::ArrowCursor);
        QMessageBox::information(this, "SUCCESS", "服务器连接成功！", "确定");
        r->exec();
    }
    else{
        setCursor(Qt::ArrowCursor);
        QMessageBox::critical(this, "ERROR", "服务器被修空调的大爷搬走了o(ﾟДﾟ)っ\n请联系管理员！\n1422872485@qq.com\n1195090535@qq.com\n2641439573@qq.com", "确定");
    }
}

void MainWindow::on_mainwindow_quit_clicked()
{
    if(dbconn.isOpen())
        this->dbconn.close();
    this->close();
}
